package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.FlowerDao;
import com.dc.model.Flower;
import com.dc.pojo.FlowerClassify;
import com.dc.service.FlowerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowerServiceImpl implements FlowerService {
//    对类成员变量、方法及构造函数进行标注,完成自动装配的工作,消除get set
    @Autowired
    FlowerDao flowerDao;
    // 分页查询花卉信息(带搜索)
    public BaseModel selectPageFlower(Integer curr_page, Integer page_size, BaseModel baseModel, String flower_name,Integer classify_id) {
            // 编写业务，实现分页 limit   (mybatis分页工具)
            PageHelper.startPage(curr_page,page_size);
            // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
            List<Flower> flowers=flowerDao.selectPageFlower(flower_name,classify_id);
            // 在new PageInfo<>(list)的时候可以把页码、页大小、总页数等信息给pageinfo，navigate导航
            PageInfo pageInfo=new PageInfo(flowers,5);
            // 设置结果code
            baseModel.setResultCode(0);
            // 设置消息提示
            baseModel.setMessage("分页查询花卉成功！");
            // 插入数据
            baseModel.setData(pageInfo);
            // 返回basemodel
            return baseModel;
    }

    // 新增花卉的接口
    public BaseModel addFlower(Flower flower, BaseModel baseModel) throws Exception {
        // 判断是否本花卉已经添加
        Integer flowerS = flowerDao.selectFlowerByName(flower.getFlower_name());
        if(flowerS>0){
            // 有花卉不准插入
            throw new BusinessException(ErrorMessageEnum.REPEAT_CLIENT);
        }
        //调用新增花卉的dao
        Integer result=flowerDao.insertFlower(flower);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增花卉成功！");
        return baseModel;
    }

    // 修改花卉的接口
    public BaseModel updateFlower(Flower flower, BaseModel baseModel) throws Exception {
        Integer result=flowerDao.updateFlower(flower);
        if(result<=0){
            throw  new BusinessException(1008,"修改花卉失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改花卉成功！");
        return baseModel;
    }

    // 删除花卉的接口
    public BaseModel deleteFlower(String ids, BaseModel baseModel) throws Exception {
        String[] idArry=ids.split(",");
        int result=flowerDao.deleteFlower(idArry);
        if(result<=0){
            throw new BusinessException(1009,"删除"+result+"个花卉失败。");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"个花卉成功！");
        return baseModel;
    }

    // 根据id查询花卉信息的接口
    public BaseModel selectFlowerById(Integer flower_id,BaseModel baseModel) throws Exception{
        List<Flower> flowerList = flowerDao.selectFlowerById(flower_id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询单个花卉成功");
        baseModel.setData(flowerList);
        return baseModel;
    }

    public BaseModel selectFlowerClassify(Integer curr_page, Integer page_size, BaseModel baseModel, String flower_name, Integer classify_id) {
        // 编写业务，实现分页 limit   (mybatis分页工具)
        PageHelper.startPage(curr_page,page_size);
        // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
        List<FlowerClassify> flowers=flowerDao.selectFlowerClassify(flower_name,classify_id);
        // 在new PageInfo<>(list)的时候可以把页码、页大小、总页数等信息给pageinfo，navigate导航
        PageInfo pageInfo=new PageInfo(flowers,5);
        // 设置结果code
        baseModel.setResultCode(0);
        // 设置消息提示
        baseModel.setMessage("分页查询花卉成功！");
        // 插入数据
        baseModel.setData(pageInfo);
        // 返回basemodel
        return baseModel;
    }
}
